package com.simple.system.service;

import com.simple.system.entity.SysMenu;
import com.simple.system.repository.SysMenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysMenuService {
    @Autowired
    private SysMenuRepository menuRepository;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public List<SysMenu> getMenuList() {
        String key = "menu:list";
        List<SysMenu> menuList = (List< SysMenu>) redisTemplate.opsForValue().get(key);
        if (menuList == null) {
            menuList = menuRepository.findAll();
            if (!menuList.isEmpty()) {
                redisTemplate.opsForValue().set(key, menuList, 1, TimeUnit.HOURS);
            }
        }
        return menuList;
    }
}    